package com.java110.mall.goods.cmd.product;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.inoutOrder.InoutOrderProductDto;
import com.java110.dto.product.ProductDto;
import com.java110.intf.cart.IInoutOrderProductInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "product.queryProductStock")
public class QueryProductStockCmd extends Cmd {

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IInoutOrderProductInnerServiceSMO inoutOrderProductInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        Assert.hasKeyAndValue(reqJson,"shopId","未包含商铺");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        ProductDto productDto = BeanConvertUtil.covertBean(reqJson, ProductDto.class);
        int count = productInnerServiceSMOImpl.queryProductAndSpecCount(productDto);

        List<ProductDto> productDtos = null;
        if (count > 0) {
            productDtos = productInnerServiceSMOImpl.queryProductAndSpecs(productDto);

            freshProductStockDtos(productDtos, productDto);
        } else {
            productDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) productDto.getRow()), count, productDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);
    }

    /**
     * 刷入 进货中 退货中
     *
     * @param productDtos
     * @param productDto
     */
    private void freshProductStockDtos(List<ProductDto> productDtos, ProductDto productDto) {
        if (ListUtil.isNull(productDtos)) {
            return;
        }
        List<String> valueIds = new ArrayList<>();
        for (ProductDto tmpProductDto : productDtos) {
            valueIds.add(tmpProductDto.getValueId());
        }

        //查询进货中
        InoutOrderProductDto inoutOrderProductDto = new InoutOrderProductDto();
        inoutOrderProductDto.setValueIds(valueIds.toArray(new String[valueIds.size()]));
        inoutOrderProductDto.setShopId(productDtos.get(0).getShopId());
        List<InoutOrderProductDto> inoutOrderProductDtos = inoutOrderProductInnerServiceSMOImpl.queryProductInoutCount(inoutOrderProductDto);

        if (ListUtil.isNull(inoutOrderProductDtos)) {
            return;
        }

        for (ProductDto tmpProductDto : productDtos) {
            for (InoutOrderProductDto inoutOrderProductDto1 : inoutOrderProductDtos) {
                if (!tmpProductDto.getValueId().equals(inoutOrderProductDto1.getValueId())) {
                    continue;
                }

                tmpProductDto.setInCount(inoutOrderProductDto1.getInCount());
                tmpProductDto.setOutCount(inoutOrderProductDto1.getOutCount());
            }
        }
    }

}
